<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="98px">
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入商品名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="商品型号" prop="model">
        <el-input v-model="queryParams.model" placeholder="请输入商品型号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <!--<el-form-item label="商品品牌" prop="brand">
        <el-input v-model="queryParams.brand" placeholder="请输入商品品牌"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>-->
      <!--<el-form-item label="商品入库单位" prop="unit">
          <el-input v-model="queryParams.unit" placeholder="请输入商品入库单位"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="采购单价" prop="costPrice">
          <el-input v-model="queryParams.costPrice" placeholder="请输入采购单价"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="采购数量" prop="count">
          <el-input v-model="queryParams.count" placeholder="请输入采购数量"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="采购总价" prop="cost">
          <el-input v-model="queryParams.cost" placeholder="请输入采购总价"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="有效日期" prop="validityTerm">
          <el-date-picker clearable v-model="queryParams.validityTerm"
                          type="date" value-format="yyyy-MM-dd" placeholder="请选择有效日期">
          </el-date-picker>
      </el-form-item>-->
      <!--<el-form-item label="采购人" prop="buyId">
        <el-input v-model="queryParams.buyId" placeholder="请输入采购人"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>-->
      <el-form-item label="采购人姓名" prop="buyName">
        <el-input v-model="queryParams.buyName" placeholder="请输入采购人姓名"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <!--<el-form-item label="采购日期" prop="buyDate">
        <el-date-picker clearable v-model="queryParams.buyDate"
                        type="date" value-format="yyyy-MM-dd" placeholder="请选择采购日期">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item label="采购方式" prop="buyType">
        <el-select v-model="queryParams.buyType" placeholder="采购方式" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.buy_type"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="是否报销" prop="reimburse">
        <el-select v-model="queryParams.reimburse" placeholder="是否报销" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.yes_no"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
        <!--        <el-input v-model="queryParams.reimburse" placeholder="请输入是否报销(0否/1是)" clearable @keyup.enter.native="handleQuery"/>-->
      </el-form-item>
      <!--<el-form-item label="是否取得发票" prop="invoice">
        <el-select v-model="queryParams.invoice" placeholder="是否取得发票" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.yes_no"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>-->
      <!--<el-form-item label="供应商" prop="supplierId">
        <el-input v-model="queryParams.supplierId" placeholder="请输入供应商"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>-->
      <el-form-item label="供应商名称" prop="supplierId">
        <el-select v-model="queryParams.supplierId" placeholder="供应商名称" clearable filterable style="width: 260px">
          <el-option v-for="dict in dict.type.goods_supplier"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="采购日期" prop="buyDateStr">
        <el-date-picker style="width: 215px;"
                        v-model="queryParams.buyDateStr"
                        @input="buyDateChange"
                        type="daterange"
                        value-format="yyyy-MM-dd"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['develop:GoodsOrderIn:add']">新增
        </el-button>
      </el-col>
      <!--<el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
                  @click="handleUpdate" v-hasPermi="['develop:GoodsOrderIn:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
          <el-popconfirm title="确认删除选择的数据项？" @confirm="handleDelete({})">
              <el-button slot="reference" type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                         v-hasPermi="['develop:GoodsOrderIn:remove']">删除</el-button>
          </el-popconfirm>
      </el-col>-->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini"
                   @click="handleExport" v-hasPermi="['develop:GoodsOrderIn:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="dataList" ref="table" :max-height="tableMaxHeight" @selection-change="handleSelectionChange" @sort-change="handleSortChange">
     <!-- <el-table-column type="selection" width="55" align="center"/>-->
      <!--                    <el-table-column label="${comment}" align="center" prop="id"/>-->
      <el-table-column label="商品名称" align="center" prop="name"/>
      <el-table-column label="商品品牌" align="center" prop="brand"/>
      <el-table-column label="商品型号" align="center" prop="model"/>
      <el-table-column label="商品单位" align="center" prop="unit" width="50px"/>
      <el-table-column label="含税单价" align="center" prop="costPriceTax"/>
      <el-table-column label="采购数量" align="center" prop="count"/>
      <el-table-column label="库存数量" align="center" prop="remainderCount" sortable/>
      <!--      <el-table-column label="采购总价" align="center" prop="cost"/>-->
      <el-table-column label="含税总价" align="center" prop="costTax"/>
      <!--<el-table-column label="有效日期" align="center" prop="validityTerm" width="100" sortable="custom" :sort-orders="['descending','ascending']" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validityTerm, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
      <!--      <el-table-column label="采购人" align="center" prop="buyId"/>-->
      <el-table-column label="采购人姓名" align="center" prop="buyName"/>
      <el-table-column label="采购日期" align="center" prop="buyDate" width="100" sortable >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.buyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="采购方式" align="center" prop="buyType" width="50px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.buy_type" :value="scope.row.buyType"/>
        </template>
      </el-table-column>
      <el-table-column label="是否报销" align="center" prop="reimburse" width="50px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.reimburse"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="供应商" align="center" prop="supplierId"/>-->
      <el-table-column label="供应商名称" align="center" prop="supplierName"/>
      <!--<el-table-column label="是否取得发票" align="center" prop="invoice">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.invoice"/>
        </template>
      </el-table-column>
      <el-table-column label="发票编号" align="center" prop="invoiceNo"/>-->
      <!--            <el-table-column label="备注" align="center" prop="remark"/>-->
      <el-table-column label="操作"  fixed="right"  align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-info" v-if="scope.row.count != scope.row.remainderCount"
                     @click="handleView(scope.row)">详情
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-info" v-if="scope.row.count != scope.row.remainderCount"
                     @click="handleViewSaleRecord(scope.row)">销售记录
          </el-button>
          <template v-if="scope.row.imburseId == null || scope.row.imburseId == ''">
            <el-button size="mini" type="text" icon="el-icon-edit"
                       v-if="scope.row.count == scope.row.remainderCount && $store.getters.name == scope.row.createBy"
                       @click="handleUpdate(scope.row)" v-hasPermi="['develop:GoodsOrderIn:edit']">修改
            </el-button>
          </template>
          <template v-if="scope.row.imburseId == null || scope.row.imburseId == ''">
            <el-popconfirm :title="`确认删除 `+scope.row.name+` 数据项？`"
                           v-if="scope.row.count == scope.row.remainderCount && $store.getters.name == scope.row.createBy"
                           @confirm="handleDelete(scope.row)" style="margin-left: 10px;">
              <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                         v-hasPermi="['develop:GoodsOrderIn:remove']">删除
              </el-button>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <goodsOrderIn-form ref="form" @ok="getList"></goodsOrderIn-form>
    <goods-sale-list-dialog-for-order ref="GoodsSaleListDialogForOrder"></goods-sale-list-dialog-for-order>
  </div>
</template>

<script>
    import {deleteAction, getAction, downloadFile} from '@/api/manage';
    import GoodsOrderInForm from '@/views/develop/goodsOrderIn/module/GoodsOrderInForm';
    import GoodsSaleListDialogForOrder from "./module/GoodsSaleListDialogForOrder";

    export default {
        name: "GoodsOrderInList",
        dicts: ['yes_no', 'buy_type', 'goods_supplier'],
        components: {
            GoodsOrderInForm,
            GoodsSaleListDialogForOrder
        },
        data() {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 入库信息管理表格数据
                dataList: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 20,
                    name: null,
                    brand: null,
                    unit: null,
                    costPrice: null,
                    count: null,
                    cost: null,
                    validityTerm: null,
                    buyId: null,
                    buyName: null,
                    buyDate: null,
                    buyType: null,
                    reimburse: null,
                    supplierId: null,
                    supplierName: null,
                    params:{},
                    orderByColumn:"create_time",
                    isAsc:"desc"
                },
                url: {
                    list: '/goodsOrderIn/goodsOrderIn/list',
                    remove: '/goodsOrderIn/goodsOrderIn/',
                    export: '/goodsOrderIn/goodsOrderIn/export',
                },
                buyDateStr: "",
                tableMaxHeight: window.innerHeight - 320,
            };
        },
        created() {
            this.getList();
            window.addEventListener('resize', this.getTableHeight);
        },
        destroyed () {
            window.removeEventListener('resize', this.getTableHeight)
        },
        mounted() {
            this.$nextTick(()=>{
                this.getTableHeight();
            })
        },
        methods: {
            handleViewSaleRecord(row) {
                this.$refs.GoodsSaleListDialogForOrder.showSaleRecordForOrder(row);
            },
            getTableHeight () {
                this.tableHeight = window.innerHeight - 320
            },
            handleSortChange(column){
              let that = this;
              that.queryParams.orderByColumn = column.prop;
              that.queryParams.isAsc = column.order;
              that.getList();
            },
            buyDateChange(val) {
                let that = this;
                that.$nextTick(() => {
                    if (that.queryParams.buyDateStr != null && that.queryParams.buyDateStr.length > 0) {
                        that.queryParams.buyDateStr = null;
                        that.$set(that.queryParams, "buyDateStr", [val[0], val[1]]);
                        that.queryParams.params.buyDateBeg = val[0];
                        that.queryParams.params.buyDateEnd = val[1];
                    } else {
                        this.queryParams.buyDateStr = null;
                        this.queryParams.params.buyDateBeg = null;
                        this.queryParams.params.buyDateEnd = null;
                    }
                });
            },
            /** 查询入库信息管理列表 */
            getList() {
                this.loading = true;
                getAction(this.url.list, this.queryParams).then(response => {
                    this.dataList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.queryParams.params.buyDateBeg = null;
                this.queryParams.params.buyDateEnd = null;
                this.queryParams.orderByColumn = "createTime";
                this.queryParams.isAsc = "desc";
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.$refs.form.handleAdd();
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.$refs.form.handleUpdate(row);
            },
            handleView(row) {
                this.$refs.form.handleView(row);
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                deleteAction(this.url.remove, ids).then(response => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {
                });
            },
            /** 导出按钮操作 */
            handleExport() {
                let that = this;
                that.loading = false;
                that.$confirm('是否确认导出商品入库信息表?', "确认", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    downloadFile('/goodsOrderIn/goodsOrderIn/exportNormal', that.queryParams);
                    /*request({
                        url: '/goodsOrderIn/goodsOrderIn/exportNormal',
                        method: 'get',
                        params: that.queryParams,
                        responseType: 'blob'
                    }).then(response => {
                        // that.saveBolbAsFile(response, "商品入库信息表.xlsx");
                        let binaryData = [];
                        binaryData.push(response.blob);
                        let link = document.createElement('a');
                        link.href = window.URL.createObjectURL(new Blob(binaryData));
                        link.download = "商品入库信息表.xlsx";
                        link.click();
                        //释放内存
                        window.URL.revokeObjectURL(link.href);

                    })*/
                });
                /*this.download(this.url.export, {
                    ...this.queryParams
                }, `goodsOrderIn_${new Date().getTime()}.xlsx`)*/
            },
        }
    };
</script>
